package com.ruoyi.course.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.vo.ExamInvoiceHeaderVo;
import com.ruoyi.course.service.IExamInvoiceHeaderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 发票抬头管理Controller
 *
 * @author Tellsea
 * @date 2025-10-22
 */
@Api(tags = "发票抬头管理Controller")
@RestController
@RequestMapping("/course/invoiceHeader")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamInvoiceHeaderController extends BaseController {

    private final IExamInvoiceHeaderService examInvoiceHeaderService;

    @ApiOperation("查询发票抬头管理列表")
    @GetMapping("/list")
    public TableDataInfo<ExamInvoiceHeaderVo> list(ExamInvoiceHeaderVo entity) {
        return examInvoiceHeaderService.queryList(entity);
    }

    @ApiOperation("查询发票抬头管理所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamInvoiceHeaderVo entity) {
        return AjaxResult.success("查询成功", examInvoiceHeaderService.queryAll(entity));
    }

    @ApiOperation("导出发票抬头管理列表")
    @Log(title = "发票抬头管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamInvoiceHeaderVo entity) {
        List<ExamInvoiceHeaderVo> list = examInvoiceHeaderService.queryAll(entity);
        ExcelUtil<ExamInvoiceHeaderVo> util = new ExcelUtil<>(ExamInvoiceHeaderVo.class);
        util.exportExcel(response, list, "发票抬头管理数据");
    }

    @ApiOperation("获取发票抬头管理详细信息")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", examInvoiceHeaderService.queryById(id));
    }

    @ApiOperation("新增发票抬头管理")
    @Log(title = "发票抬头管理", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamInvoiceHeaderVo entity) {
        entity.setUserId(getUserId());
        return toAjax(examInvoiceHeaderService.save(entity));
    }

    @ApiOperation("修改发票抬头管理")
    @Log(title = "发票抬头管理", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamInvoiceHeaderVo entity) {
        return toAjax(examInvoiceHeaderService.updateById(entity));
    }

    @ApiOperation("删除发票抬头管理")
    @Log(title = "发票抬头管理", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examInvoiceHeaderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @ApiOperation("获取当前用户的发票抬头列表")
    @PostMapping("/myList")
    public AjaxResult myList(@RequestBody ExamInvoiceHeaderVo entity) {
        // 设置当前用户ID
        entity.setUserId(getUserId());
        List<ExamInvoiceHeaderVo> list = examInvoiceHeaderService.queryAll(entity);
        return AjaxResult.success("查询成功", list);
    }

    
}
